﻿using MesBSW.BLL.Mes;
using MesBSW.BLL.Role;
using MesBSW.DAL.CustomDB;
using MesBSW.DAL.MesDB;
using MesBSW.DAL.MesDTO;
using MesBSW.DAL.RoleDB;
using MesBSW.View.Dialog.Language;
using NPOI.POIFS.Properties;
using NPOI.SS.Formula.Functions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesBSW.View.Language
{
    public partial class LanguageSettingFrm : UIPage
    {
        public LanguageSettingFrm()
        {
            InitializeComponent();
        }

        #region "内部变量"
        private LanguageControler _lang_ctrl = new LanguageControler();
        private HomeControler _home_ctrl = new HomeControler();

        private List<LanguageInfo> _lang_info_list = new List<LanguageInfo>();
        private List<LanguageMulitDTO> _lang_mulit_list = new List<LanguageMulitDTO>();
        #endregion

        #region "控件事件"
        /// <summary>
        /// 界面读入时读入相关数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LanguageSettingFrm_Load(object sender, EventArgs e)
        {
            //读取现有的所有语言类型
            this._lang_info_list = this._lang_ctrl.SearchAllLanguageInfo();

            //读取现有的所有多语言设置
            this._lang_mulit_list = this._lang_ctrl.SearchLanguageMulitForType(null);

            //初始化界面左边的界面列表
            this.InitLeftMenuBar();

            //根据语言种类初始化界面右边的多语言列表栏位
            this.InitDataGridViewColumn(this._lang_info_list);
        }

        /// <summary>
        /// 在界面左边的树控件中选择窗口名，右边显示该窗口的所有控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwForm_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag != null && string.IsNullOrEmpty(e.Node.Tag.ToString()) == false)
            {
                this.LoadIngControlForClassName(e.Node.Tag.ToString());
            }
        }

        /// <summary>
        /// 保存多语言设定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            List<LanguageItemDTO> saveList = new List<LanguageItemDTO>();
            if (this.dgvLanguage.Rows.Count > 0)
            {
                DataTable tmpDt = this.dgvLanguage.DataSource as DataTable;
                tmpDt.EndInit();
                foreach (DataGridViewRow viewRow in this.dgvLanguage.Rows)
                {
                    DataRow tmpRow = (viewRow.DataBoundItem as DataRowView).Row;
                    LanguageItemDTO tmpBean = this.ChangeLanguageItemDtoForDataRow(tmpRow);
                    if (tmpBean != null)
                    {
                        saveList.Add(tmpBean);
                    }
                }
            }

            if (saveList != null && saveList.Count > 0)
            {
                try
                {
                    string className = saveList[0].form_name;
                    int count = this._lang_ctrl.SaveLanguageItem(saveList);
                    UIMessageBox.ShowSuccess("多语言设置保存成功！");
                    this._lang_mulit_list = this._lang_ctrl.SearchLanguageMulitForType(null);
                    this.LoadIngControlForClassName(className);
                }
                catch (Exception ex)
                {
                    UIMessageBox.ShowError("多语言设置保存失败！" + "\r\n" + ex.Message);
                }
            }
            else
            {
                UIMessageBox.ShowWarning("没有任何可以保存多语言资料！");
            }
        }


        /// <summary>
        /// 设换界面时，判断是否有保存，并提示用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwForm_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (this.dgvLanguage.DataSource != null)
            {
                DataTable tmpDt = this.dgvLanguage.DataSource as DataTable;
                if (tmpDt != null && tmpDt.Rows.Count > 0)
                {
                    bool isChange = false;
                    foreach (DataRow dr in tmpDt.Rows)
                    {
                        if (dr.RowState != DataRowState.Unchanged)
                        {
                            isChange = true;
                        }
                    }

                    if (isChange)
                    {
                        if (MessageBox.Show("已修改该界面的多语言设定，是否需要保存？", "是否保存", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            this.btnSave_Click(sender, e);
                        }
                    }
                }
            }
        }
        #endregion

        #region "内部方法"

        /// <summary>
        /// 设定界面中DataGridView的栏位
        /// </summary>
        /// <param name="infoList"></param>
        private void InitDataGridViewColumn(List<LanguageInfo> infoList)
        {
            this.dgvLanguage.Columns.Clear();
            //先设定语言项目的固定栏位的属性
            List<DgvColumnInfo> cluInfoList = new List<DgvColumnInfo> { 
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "index_dgv1",
                    column_visible = true,
                    column_width = 40,
                    header_text = "序号",
                    data_property_name = "index",
                    read_only = true
                },
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "item_id_dgv1",
                    column_visible = false,
                    column_width = 40,
                    header_text = "item_id",
                    data_property_name = "item_id",
                    read_only = true
                },
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "item_type_dgv1",
                    column_visible = false,
                    column_width = 40,
                    header_text = "item_type",
                    data_property_name = "item_type",
                    read_only = true
                },
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "form_name_dgv1",
                    column_visible = false,
                    column_width = 150,
                    header_text = "类名",
                    data_property_name = "form_name",
                    read_only = true
                },
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "ctrl_name_dgv1",
                    column_visible = true,
                    column_width = 120,
                    header_text = "控件名",
                    data_property_name = "ctrl_name",
                    read_only = true
                },
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "ctrl_class_dgv1",
                    column_visible = true,
                    column_width = 120,
                    header_text = "控件类名",
                    data_property_name = "ctrl_class",
                    read_only = true
                },
                new DgvColumnInfo
                {
                    column_type = typeof(DataGridViewTextBoxColumn),
                    column_name = "ctrl_text_dgv1",
                    column_visible = true,
                    column_width = 120,
                    header_text = "控件文本",
                    data_property_name = "ctrl_text",
                    read_only = true
                },
                new DgvCheckBoxColumnInfo
                {
                    column_type = typeof(DataGridViewCheckBoxColumn),
                    column_name = "is_mulit_lang_dgv1",
                    column_visible = true,
                    column_width = 50,
                    header_text = "是否需要翻译",
                    data_property_name = "is_mulit_lang",
                    read_only = false,
                    false_value = 0,
                    true_value = 1
                },
            };

            //动态设定所有语言种类的栏位的属性
            if (infoList != null && infoList.Count > 0)
            {
                int count = 0;
                foreach (var langType in infoList.OrderBy(q => q.id))
                {
                    List<DgvColumnInfo> tmpList = new List<DgvColumnInfo>
                    {
                        new DgvColumnInfo
                        {
                            column_type = typeof(DataGridViewTextBoxColumn),
                            column_name = $"info_id_{(count + 1).ToString("D2")}_dgv1",
                            column_visible = false,
                            column_width = 40,
                            header_text = $"info_id_{(count + 1).ToString("D2")}",
                            data_property_name = $"info_id_{(count + 1).ToString("D2")}",
                            read_only = true
                        },
                        new DgvColumnInfo
                        {
                            column_type = typeof(DataGridViewTextBoxColumn),
                            column_name = $"info_code_{(count + 1).ToString("D2")}_dgv1",
                            column_visible = false,
                            column_width = 40,
                            header_text = $"info_code_{(count + 1).ToString("D2")}",
                            data_property_name = $"info_code_{(count + 1).ToString("D2")}",
                            read_only = true
                        },
                        new DgvColumnInfo
                        {
                            column_type = typeof(DataGridViewTextBoxColumn),
                            column_name = $"info_name_{(count + 1).ToString("D2")}_dgv1",
                            column_visible = false,
                            column_width = 40,
                            header_text = $"info_name_{(count + 1).ToString("D2")}",
                            data_property_name = $"info_name_{(count + 1).ToString("D2")}",
                            read_only = true
                        },
                        new DgvColumnInfo
                        {
                            column_type = typeof(DataGridViewTextBoxColumn),
                            column_name = $"mulit_id_{(count + 1).ToString("D2")}_dgv1",
                            column_visible = false,
                            column_width = 40,
                            header_text = $"mulit_id_{(count + 1).ToString("D2")}",
                            data_property_name = $"mulit_id_{(count + 1).ToString("D2")}",
                            read_only = true
                        },
                        new DgvColumnInfo
                        {
                            column_type = typeof(DataGridViewTextBoxColumn),
                            column_name = $"lang_text_{(count + 1).ToString("D2")}_dgv1",
                            column_visible = true,
                            column_width = 200,
                            header_text = $"{langType.lang_name_cn}({langType.lang_name_en})",
                            data_property_name = $"lang_text_{(count + 1).ToString("D2")}",
                            read_only = false
                        }
                    };
                    count++;

                    cluInfoList.AddRange(tmpList);
                }
            }

            //根据设定生成栏位
            if (cluInfoList != null && cluInfoList.Count > 0)
            {
                foreach (var clu in cluInfoList)
                {
                    DataGridViewColumn instance = Activator.CreateInstance(clu.column_type) as DataGridViewColumn;
                    if (instance != null)
                    {
                        instance.Name = clu.column_name;
                        instance.Width = clu.column_width;
                        instance.HeaderText = clu.header_text;
                        instance.Visible = clu.column_visible;
                        instance.ReadOnly = clu.read_only;
                        instance.DataPropertyName = clu.data_property_name;
                        if (clu.column_type.Name.Contains("DataGridViewCheckBoxColumn"))
                        {
                            ((DataGridViewCheckBoxColumn)instance).FalseValue = ((DgvCheckBoxColumnInfo)clu).false_value;
                            ((DataGridViewCheckBoxColumn)instance).TrueValue = ((DgvCheckBoxColumnInfo)clu).true_value;
                        }
                        this.dgvLanguage.Columns.Add(instance);
                    }
                }
                this.dgvLanguage.ColumnHeadersHeight = 50;
            }
        }


        /// <summary>
        /// 初始化界面左边的菜单树
        /// </summary>
        private void InitLeftMenuBar()
        {
            //清空树控件的所有节点
            this.tvwForm.Nodes.Clear();

            //添加窗口节点
            TreeNode formNode = new TreeNode();
            formNode.Tag = "";
            formNode.Text = "窗口（Form）";
            List<TreeNode> formList = this.InitTreeNodeForWinForm();
            if (formList != null && formList.Count > 0) 
            {
                foreach (TreeNode node in formList)
                {
                    formNode.Nodes.Add(node);
                }
            }
            this.tvwForm.Nodes.Add(formNode);

            //添加对话框节点
            TreeNode diaNode = new TreeNode();
            diaNode.Tag = "";
            diaNode.Text = "对话框（Dia）";
            List<TreeNode> diaList = this.InitTreeNodeForDialogue();
            if (diaList != null && diaList.Count > 0)
            {
                foreach (TreeNode node in diaList)
                {
                    diaNode.Nodes.Add(node);
                }
            }
            this.tvwForm.Nodes.Add(diaNode);
        }

        /// <summary>
        /// 使用权限设置，取得所有窗口表单的节点
        /// </summary>
        /// <returns></returns>
        private List<TreeNode> InitTreeNodeForWinForm()
        {
            List<TreeNode> resList = new List<TreeNode>();
            List<BaseMenuModel> menuList = this._home_ctrl.SearchAllBaseMuneForProg(5);
            if (menuList != null && menuList.Count > 0)
            {
                var parentQuery = menuList.Where(q => q.parent_id == 0);
                if (parentQuery != null && parentQuery.Count() > 0)
                {
                    foreach (var parentItem in parentQuery.OrderBy(q => q.sort))
                    {
                        TreeNode parentNode = this.FindChildWinForm(parentItem, menuList);
                        resList.Add(parentNode);    
                    }
                }
            }
            return resList;
        }

        /// <summary>
        /// 递归取得窗口表单的所有子接点
        /// </summary>
        /// <param name="parentMenu"></param>
        /// <param name="menuList"></param>
        /// <returns></returns>
        private TreeNode FindChildWinForm(BaseMenuModel parentMenu, List<BaseMenuModel> menuList)
        {
            TreeNode resNode = new TreeNode();
            resNode.Tag = string.IsNullOrEmpty(parentMenu.class_name) ? "" : parentMenu.class_name;
            resNode.Text = string.IsNullOrEmpty(parentMenu.menu_name) ? "" : parentMenu.menu_name;

            var childQuery = menuList.Where(q => q.parent_id == parentMenu.id);
            if (childQuery != null && childQuery.Count() > 0)
            {
                foreach (var childItem in childQuery.OrderBy(q => q.sort))
                {
                    TreeNode childNode = FindChildWinForm(childItem, menuList);
                    resNode.Nodes.Add(childNode);
                }
            }

            return resNode;
        }


        /// <summary>
        /// 通过反射，取得所有对话框的种类
        /// </summary>
        /// <returns></returns>
        private List<TreeNode> InitTreeNodeForDialogue()
        {
            List<TreeNode> resList = new List<TreeNode>();
            Assembly assembly = Assembly.GetExecutingAssembly();
            var tmpList = assembly.GetTypes();
            foreach (var type in tmpList)
            {
                if (type.Name.Contains("Dia"))
                {
                    TreeNode tmpNode = new TreeNode();
                    tmpNode.Tag = type.FullName;
                    object instance = Activator.CreateInstance(type);

                    // 如果需要，可以将实例转换为具体类型
                    var yourInstance = instance as Form;

                    tmpNode.Tag = type.FullName;
                    tmpNode.Text = yourInstance.Text;
                    resList.Add(tmpNode);
                }
            }
            return resList;
        }

        /// <summary>
        /// 递归取得父控件下的所有子控件的信息
        /// </summary>
        /// <param name="className"></param>
        /// <param name="parent"></param>
        /// <param name="itemList"></param>
        private void GetAllControlForForm(string className, Control parent, ref List<LanguageItem> itemList)
        {
            if (parent != null && parent.Controls != null && parent.Controls.Count > 0)
            {
                foreach (var tmpControl in parent.Controls)
                {
                    if ((tmpControl as Control) != null)
                    {
                        Console.WriteLine(tmpControl.GetType().FullName);
                        LanguageItem tmpItem = new LanguageItem
                        {
                            id = 0,
                            item_type = "ctrl",
                            form_name = className,
                            ctrl_name = ((Control)tmpControl).Name,
                            ctrl_text = ((Control)tmpControl).Text,
                            ctrl_class = tmpControl.GetType().Name,
                            is_mulit_lang = 1
                        };
                        itemList.Add(tmpItem);

                        if (tmpControl.GetType().FullName.Contains("DataGridView"))
                        {
                            DataGridView tmpDgv = tmpControl as DataGridView;
                            foreach (DataGridViewColumn clu in tmpDgv.Columns)
                            {
                                LanguageItem cluItem = new LanguageItem
                                {
                                    id = 0,
                                    item_type = "ctrl",
                                    form_name = className,
                                    ctrl_name = clu.Name,
                                    ctrl_text = clu.HeaderText,
                                    ctrl_class = clu.GetType().Name,
                                    is_mulit_lang = 1
                                };
                                itemList.Add(cluItem);
                            }
                        }

                        //递归获取子控件
                        GetAllControlForForm(className, (Control)tmpControl, ref itemList);
                    }
                }
            }
        }


        /// <summary>
        /// 将获取到的控件与所有现有语言做绑定关系
        /// </summary>
        /// <param name="allTypeList"></param>
        /// <param name="itemList"></param>
        /// <returns></returns>
        private List<LanguageMulitDTO> InitLanguageMulitForForm(List<LanguageInfo> allTypeList, List<LanguageItem> itemList)
        {
            List<LanguageMulitDTO> resList = new List<LanguageMulitDTO>();
            if (itemList != null && itemList.Count > 0 && allTypeList != null && allTypeList.Count > 0)
            {
                //循环每个控件、和语言项
                foreach (LanguageItem tmpItem in itemList)
                {
                    foreach (LanguageInfo tmpType in allTypeList)
                    {
                        LanguageMulitDTO langBean = new LanguageMulitDTO();

                        //检查控件是否已经存在于数据库
                        LanguageItem itemBean = tmpItem;
                        if (this._lang_mulit_list != null && this._lang_mulit_list.Count > 0)
                        {
                            var isExQuery = this._lang_mulit_list.Where(q => q.LanguageItems.form_name == tmpItem.form_name && q.LanguageItems.ctrl_name == tmpItem.ctrl_name);
                            itemBean = isExQuery != null && isExQuery.Count() > 0 ? isExQuery.First().LanguageItems : tmpItem;
                        }

                        //检查该控件与语言是否已经存在于数据库，如果是，则调出数据库的资料
                        if (this._lang_mulit_list != null && this._lang_mulit_list.Count > 0)
                        {
                            var isExQuery = this._lang_mulit_list.Where(q => q.LanguageInfos.id == tmpType.id)
                                                                .Where(q => q.LanguageItems.form_name == tmpItem.form_name && q.LanguageItems.ctrl_name == tmpItem.ctrl_name);
                            langBean = isExQuery != null && isExQuery.Count() > 0 ? isExQuery.First() : new LanguageMulitDTO();
                        }

                        //如果不存在，则初始化数据
                        if (langBean is null || langBean.id <= 0)
                        {
                            langBean = new LanguageMulitDTO
                            {
                                id = 0,
                                lang_info_id = tmpType.id,
                                lang_item_id = tmpItem != null && tmpItem.id > 0 ? tmpItem.id : 0,
                                lang_text = "",
                                LanguageInfos = tmpType,
                                LanguageItems = itemBean
                            };
                            if (tmpType.lang_name_cn.Equals("默认") && string.IsNullOrEmpty(tmpItem.ctrl_text) == false)
                            {
                                langBean.lang_text = tmpItem.ctrl_text;

                            }
                        }

                        resList.Add(langBean);
                    }
                }
            }
            return resList;
        }

        /// <summary>
        /// 将语言设置由List转化成DataTable
        /// </summary>
        /// <param name="mulitList"></param>
        /// <returns></returns>
        private DataTable ChangeLanguageMulitToDataTable(List<LanguageMulitDTO> mulitList)
        {
            DataTable resDt = new DataTable();
            resDt.Columns.Add("index", typeof(int));
            resDt.Columns.Add("item_id", typeof(int));
            resDt.Columns.Add("item_type", typeof(string));
            resDt.Columns.Add("form_name", typeof(string));
            resDt.Columns.Add("ctrl_name", typeof(string));
            resDt.Columns.Add("ctrl_class", typeof(string));
            resDt.Columns.Add("ctrl_text", typeof(string));
            resDt.Columns.Add("is_mulit_lang", typeof(int));

            int cluIndex = 1;
            if (this._lang_info_list != null)
            {
                foreach (var infoBean in this._lang_info_list.OrderBy(q => q.id))
                {
                    resDt.Columns.Add($"info_id_{cluIndex.ToString("D2")}", typeof(int));
                    resDt.Columns.Add($"info_code_{cluIndex.ToString("D2")}", typeof(string));
                    resDt.Columns.Add($"info_name_{cluIndex.ToString("D2")}", typeof(string));
                    resDt.Columns.Add($"mulit_id_{cluIndex.ToString("D2")}", typeof(int));
                    resDt.Columns.Add($"lang_text_{cluIndex.ToString("D2")}", typeof(string));
                    cluIndex++; 
                }
            }

            if (mulitList != null && mulitList.Count > 0)
            {
                var itemList = mulitList.OrderBy(q => q.LanguageItems.ctrl_class)
                                            .Select(q => new { form = q.LanguageItems.form_name, ctrl = q.LanguageItems.ctrl_name }).Distinct().ToList();
                int dataIndex = 1;
                foreach (var item in itemList)
                {
                    var tmpQuery = mulitList.Where(q => q.LanguageItems.form_name == item.form && q.LanguageItems.ctrl_name == item.ctrl).OrderBy(q => q.LanguageInfos.id);
                    var tmpList = tmpQuery.ToList();
                    DataRow addRow = resDt.NewRow();
                    addRow["index"] = dataIndex;
                    addRow["item_id"] = tmpQuery.First().lang_item_id;
                    addRow["item_type"] = tmpQuery.First().LanguageItems.item_type;
                    addRow["form_name"] = tmpQuery.First().LanguageItems.form_name;
                    addRow["ctrl_name"] = tmpQuery.First().LanguageItems.ctrl_name;
                    addRow["ctrl_class"] = tmpQuery.First().LanguageItems.ctrl_class;
                    addRow["ctrl_text"] = tmpQuery.First().LanguageItems.ctrl_text;
                    addRow["is_mulit_lang"] = tmpQuery.First().LanguageItems.is_mulit_lang;

                    int index = 1;
                    foreach (var infoBean in this._lang_info_list.OrderBy(q => q.id))
                    {
                        var childQuery = tmpList.Where(q => q.LanguageInfos.lang_code == infoBean.lang_code);
                        if (childQuery != null && childQuery.Count() > 0)
                        {
                            addRow[$"info_id_{index.ToString("D2")}"] = childQuery.First().LanguageInfos.id;
                            addRow[$"info_code_{index.ToString("D2")}"] = childQuery.First().LanguageInfos.lang_code;
                            addRow[$"info_name_{index.ToString("D2")}"] = childQuery.First().LanguageInfos.lang_name_cn;
                            addRow[$"mulit_id_{index.ToString("D2")}"] = childQuery.First().id;
                            addRow[$"lang_text_{index.ToString("D2")}"] = childQuery.First().lang_text;
                        }
                        index++;

                    }
                    
                    resDt.Rows.Add(addRow);
                }
            }

            resDt.AcceptChanges();
            return resDt;
        }

        /// <summary>
        /// 根据类名读取该类里面的控件
        /// </summary>
        /// <param name="className"></param>
        private void LoadIngControlForClassName(string className)
        {
            Type type = Type.GetType(className);
            if (type != null)
            {
                object instance = Activator.CreateInstance(type);
                var tmpForm = instance as Form;
                List<LanguageItem> itemList = new List<LanguageItem>();
                if (tmpForm != null)
                {
                    foreach (Control parentCtrl in tmpForm.Controls)
                    {
                        this.GetAllControlForForm(className, parentCtrl, ref itemList);
                    }
                }

                List<LanguageMulitDTO> mulitList = this.InitLanguageMulitForForm(this._lang_info_list, itemList);
                this.dgvLanguage.DataSource = this.ChangeLanguageMulitToDataTable(mulitList);
            }
        }

        /// <summary>
        /// 将DataRow的记录转换成实体类
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private LanguageItemDTO ChangeLanguageItemDtoForDataRow(DataRow row)
        {
            int tmpInt = 0;

            //用DataRow的资料初始化多语言的项目实体类
            LanguageItemDTO resBean = new LanguageItemDTO
            {
                id = row["item_id"] != DBNull.Value && int.TryParse(row["item_id"].ToString(), out tmpInt) ? tmpInt : 0,
                item_type = row["item_type"] != DBNull.Value ? row["item_type"].ToString().Trim() : "",
                form_name = row["form_name"] != DBNull.Value ? row["form_name"].ToString().Trim() : "",
                ctrl_name = row["ctrl_name"] != DBNull.Value ? row["ctrl_name"].ToString().Trim() : "",
                ctrl_class = row["ctrl_class"] != DBNull.Value ? row["ctrl_class"].ToString().Trim() : "",
                ctrl_text = row["ctrl_text"] != DBNull.Value ? row["ctrl_text"].ToString().Trim() : "",
                is_mulit_lang = row["is_mulit_lang"] != DBNull.Value && int.TryParse(row["is_mulit_lang"].ToString(), out tmpInt) ? tmpInt : 0
            };

            //从DataRow中取得语言的种类
            List<string> indexList = new List<string>();
            foreach (DataColumn tmpColumn in row.Table.Columns)
            {
                if (tmpColumn.ColumnName.Length >= 2)
                {
                    string tmpString = tmpColumn.ColumnName.Substring(tmpColumn.ColumnName.Length - 2, 2);
                    if (int.TryParse(tmpString, out tmpInt) && indexList.Contains(tmpString) == false)
                    {
                        indexList.Add(tmpString);
                    }
                }
            }

            //取得各语言种类的翻译
            foreach (string index in indexList)
            {
                LanguageMulit mulitBean = new LanguageMulit
                {
                    id = row[$"mulit_id_{index}"] != DBNull.Value && int.TryParse(row[$"mulit_id_{index}"].ToString(), out tmpInt) ? tmpInt : 0,
                    lang_info_id = row[$"info_id_{index}"] != DBNull.Value && int.TryParse(row[$"info_id_{index}"].ToString(), out tmpInt) ? tmpInt : 0,
                    lang_item_id = resBean.id,
                    lang_text = row[$"lang_text_{index}"] != DBNull.Value ? row[$"lang_text_{index}"].ToString() : ""
                };
                resBean.LanguageMulits.Add(mulitBean);
            }

            return resBean;
        }

        #endregion

        private void btnAddLanguageInfo_Click(object sender, EventArgs e)
        {
            AddLanguageInfoDia tmpDia = new AddLanguageInfoDia();
            tmpDia.ShowDialog();

            //添加完语言种类后，立刻重新获取所有多语言种类
            this._lang_info_list = this._lang_ctrl.SearchAllLanguageInfo();

            //根据语言种类初始化界面右边的多语言列表栏位
            this.InitDataGridViewColumn(this._lang_info_list);

            if (this.tvwForm.SelectedNode != null && this.tvwForm.SelectedNode.Tag != null && string.IsNullOrEmpty(this.tvwForm.SelectedNode.Tag.ToString()) == false)
            {
                this.LoadIngControlForClassName(this.tvwForm.SelectedNode.Tag.ToString());
            }
        }
    }
}
